﻿using FlowCanvas.Nodes;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class CreateStreetEventVehicle : CallableActionNode<int, string, bool>
    {
        private CarEntity carEntity;
        public override void Invoke(int carModelId, string pointName, bool forceInCar)
        {
            var bluePoint = BlueprintUtils.GetBlueprintPoint(pointName.Trim());
            if (bluePoint == null)
            {
                return;
            }
            
            CarBirthData birthData = new CarBirthData();
            birthData.random = carModelId == -1;
            birthData.carModelId = carModelId;
            birthData.driveNpcId = 0;
            birthData.position = bluePoint.position;
            birthData.forward = bluePoint.forward;
            
            bool success = LibertyAIUtils.AIManager.CarBirthManager.AddCarPure(birthData) != null;
            if (!success)
            {
                return;
            }

            carEntity = LibertyAIUtils.AIManager.CarBirthManager.GetCarEntity(birthData.uniqueId);
            if (carEntity == null)
            {
                return;
            }

            if (forceInCar)
            {
                // 玩家强制进车
                LibertyAIUtils.PlayerController.vehicleBehaviourMgr.EnterCarImmediately(carEntity);
            }
        }

        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (carEntity != null)
            {
                LibertyAIUtils.AIManager.CarBirthManager.RemoveCar(carEntity.id);
            }
        }
    }
}